<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="100px" @submit.native.prevent>
      <el-form-item label="接收目标" prop="type">
        <el-select v-model="pram.type" placeholder="接收目标" clearable style="width: 100%">
          <!-- 1为系统主动触发消息 -->
<!--          <el-option :key="0" label="二级单位" :value="2"/>-->
          <el-option :key="1" label="战士" :value="3"/>
        </el-select>
      </el-form-item>
      <el-form-item label="接收单位" prop="departmentId" v-if="pram.type === 2">
        <el-cascader v-model="pram.departmentId" :disabled="pram.departmentId ===0" :options="parentOptions" :props="categoryProps" style="width:100%" />
        <!-- allTreeList or parentOptions-->
      </el-form-item>
      <el-form-item label="接收人员" prop="personId" v-if="pram.type === 3">
        <el-select v-model="pram.personId" placeholder="接收人员" clearable multiple style="width: 100%">
          <el-option
            v-for="(item,index) in personList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:message:repeat']">确定</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as roleApi from '@/api/role.js'
import * as messageApi from '@/api/message.js'
import {Debounce} from '@/utils/validate'
export default {
  // name: "edit"
  components: { },
  props: {
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allPersonList:{
      type: Array
    },
    allTreeList: {
      type: Array
    },
    allCategoryList: {
      type: Array
    },
  },
  data() {
    /*const validatePhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请填写手机号'));
      } else if (!/^1[3456789]\d{9}$/.test(value)) {
        callback(new Error('手机号格式不正确!'));
      } else {
        callback();
      }
    };*/
    return {
      constants:this.$constants,
      pram: {
        type:null,
        departmentId: null,
        personId: null,
        id: 0,
      },
      categoryId:null,
      title:null,
      content:null,
      rules: {
        type: [{ required: true, message: '请选择接受目标', trigger: ['blur', 'change'] }],
        departmentId: [{ required: true, message: '请选择接收单位', trigger: ['blur', 'change'] }],
        personId: [ { required: true, message: '请选择接收人员', trigger: ['blur', 'change']} ],
      },
      personList:[],
      categoryList:[],
      parentOptions: [],
      categoryProps: {
        value: 'id',
        label: 'name',
        children: 'child',
        expandTrigger: 'hover',
        checkStrictly: true,
        emitPath: false,
        multiple:true
      },
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideRepeatDialog')
    },

    initEditData() {
      this.personList = this.allPersonList
      this.categoryList = this.allCategoryList
      this.parentOptions = [...this.allTreeList]
      this.addTreeListLabelForCasCard(this.parentOptions, 'child')

      const { personId, id } = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.personId = personId
      this.pram.id = id
    },
    addTreeListLabelForCasCard(arr, child) {
      arr.forEach((o,i) => {
        if (o.child && o.child.length) {
          o.disabled = true
          o.child.forEach((j) => {
            //j.disabled = true
          })
        }
      })
    },
    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        this.handlerSave()
      })
    }),
    handlerSave() {
      var _pram = this.pram
      if(_pram.departmentId) _pram.departmentId = _pram.departmentId.join(',')
      if(_pram.personId) _pram.personId = _pram.personId.join(',')
      messageApi.mesAdd(this.pram).then(data => {
        this.$message.success('转发成功')
        this.$emit('hideRepeatDialog')
      })
    },
  }
}
</script>

<style scoped>

</style>
